package com.tgu.home_action.service.impl;

import com.tgu.home_action.dao.CaseMapper;
import com.tgu.home_action.dao.DynamicMapper;
import com.tgu.home_action.dao.FamilyMapper;
import com.tgu.home_action.dao.VolunteerCaseMapper;
import com.tgu.home_action.pojo.Case;
import com.tgu.home_action.pojo.Dynamic;
import com.tgu.home_action.pojo.Family;
import com.tgu.home_action.pojo.ResponseData;
import com.tgu.home_action.pojo.exception.IMException;
import com.tgu.home_action.pojo.state.DynamicId;
import com.tgu.home_action.pojo.state.RedisHeader;
import com.tgu.home_action.pojo.state.ResponseState;
import com.tgu.home_action.service.RedisService;
import com.tgu.home_action.service.utils.IMService;
import org.bouncycastle.crypto.macs.CMac;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class RedisServiceImpl implements RedisService {
    @Autowired
    private IMService imService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;
    @Autowired
    private VolunteerCaseMapper volunteerCaseMapper;
    @Autowired
    private CaseMapper caseMapper;
    @Autowired
    private FamilyMapper familyMapper;
    @Autowired
    private DynamicMapper dynamicMapper;

    /**
     * 处理离线志愿者
     */
    @Override
    public void volunteerOffLine(Integer volunteerId) {
        stringRedisTemplate.opsForSet().remove(RedisHeader.VOLUNTEER_ONLINE.getHeader(), String.valueOf(volunteerId));
        List<Integer> caseIds = volunteerCaseMapper.getIngCaseIdByVolunteerId(volunteerId);
        for (Integer caseId :
                caseIds) {
            ResponseData<Integer> responseData = new ResponseData<>(ResponseState.VOLUNTEER_OFFLINE.getMessage(), ResponseState.VOLUNTEER_OFFLINE.getValue(), volunteerId);
            simpMessagingTemplate.convertAndSend("/case/" + caseId, responseData);
        }
    }

    /**
     * 处理离线超过两小时的志愿者
     */
    @Override
    public void volunteerOffLineOutTwoHour(Integer volunteerId) {
        stringRedisTemplate.opsForGeo().remove(RedisHeader.VOLUNTEER_PLACE.getHeader(), String.valueOf(volunteerId));
        stringRedisTemplate.opsForSet().remove(RedisHeader.VOLUNTEER_ONLINE_TWO_HOUR.getHeader(), String.valueOf(volunteerId));
    }

    /**
     * 处理超时案件
     */
    @Override
    public void caseOverTime(Integer caseId) {
        caseMapper.changeCaseState(caseId, 3);
        Case nowCase = caseMapper.getById(caseId);
        try {
            Family family = familyMapper.getById(nowCase.getFamilyId());
            Dynamic dynamic = new Dynamic(nowCase.getId(), nowCase.getFamilyId(), null, DynamicId.START_CASE.getValue(), family.getName(), family.getAvatarUrl());
            dynamicMapper.insertDynamic(dynamic);
        } catch (Exception e) {
            e.printStackTrace();
        }
        ResponseData<Object> responseData = new ResponseData<>(ResponseState.CASE_OVER_TIME.getMessage(), ResponseState.CASE_OVER_TIME.getValue(), nowCase);
        simpMessagingTemplate.convertAndSend("/case/" + caseId, responseData);

    }
}
